package com.hdoit.ares.logical.cust.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.hdoit.ares.logical.cust.dao.IAdviceFeedbackDao;
import com.hdoit.ares.logical.cust.model.AdviceFeedback;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.viewmodel.cust.AdviceFeedbackSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;

@Component
public class AdviceFeedbackDaoImpl extends SuperDao<AdviceFeedback> implements IAdviceFeedbackDao {

	@Override
	public List<AdviceFeedback> queryList(
			AdviceFeedbackSearch adviceFeedbackSearch, PagerModel pagerModel) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("DISTINCT adviceFeedback.*","customer.username as adviceName")
		.from(AdviceFeedback.class,"adviceFeedback")
		.leftJoin(Customer.class, "customer")
		.on("adviceFeedback.adviceId","customer.id")
		.andWhere("adviceFeedback.deleteMark=#{deleteMark}")
		.setParam("deleteMark", YesOrNo.no)
		.setMainTableAlias("adviceFeedback");
		
		if(pagerModel == null){
			return queryList(finderExpression, adviceFeedbackSearch);
		}
		
		return queryList(finderExpression, adviceFeedbackSearch,pagerModel);
	}

	@Override
	public AdviceFeedback queryModel(String key){
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("DISTINCT adviceFeedback.*","customer.username as adviceName")
		.from(AdviceFeedback.class,"adviceFeedback")
		.leftJoin(Customer.class, "customer")
		.on("adviceFeedback.adviceId","customer.id")
		.andWhere("adviceFeedback.id=#{id}")
		.setParam("id", key)
		.andWhere("adviceFeedback.deleteMark=#{deleteMark}")
		.setParam("deleteMark", YesOrNo.no)
		.setMainTableAlias("adviceFeedback");
		
		return super.queryModel(finderExpression);
	}

	@Override
	public List<AdviceFeedback> queryList(
			AdviceFeedbackSearch adviceFeedbackSearch) {
		return queryList(adviceFeedbackSearch,null);
	}

	@Override
	public List<AdviceFeedback> queryListWechat(String adviceId) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("DISTINCT adviceFeedback.*","customer.username as adviceName")
		.from(AdviceFeedback.class,"adviceFeedback")
		.leftJoin(Customer.class,"customer")
		.on("adviceFeedback.adviceId","customer.id")
		.andWhere("adviceFeedback.adviceId=#{adviceId}")
		.setParam("adviceId", adviceId)
		.andWhere("adviceFeedback.deleteMark=#{deleteMark}")
		.setParam("deleteMark", YesOrNo.no)
		.setMainTableAlias("adviceFeedback")
		.addOrder("adviceFeedback.createDate", OrderEnum.desc);
		return queryList(finderExpression);
	}
}
